package com.xjoyt.pay.operater.services;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import com.xjoyt.pay.abstracts.AbstractService;
import com.xjoyt.pay.dto.ExecuteResult;
import com.xjoyt.pay.dto.PageParam;
import com.xjoyt.pay.dto.PageResult;
import com.xjoyt.pay.operater.auth.po.Department;
import com.xjoyt.pay.operater.repository.DepartmentRepository;

@Service
public class DepartmentService extends AbstractService {

	@Autowired
	private DepartmentRepository repository;

	public Department get(String id) {
		return repository.findOne(id);
	}

	public PageResult<Department> loadPage(PageParam<?> param) {
		Page<Department> page = repository.findAll(getPageable(param));
		return new PageResult<>(page.getTotalElements(), page.getContent());
	}

	public ExecuteResult<Department> save(Department entity) {
		if (entity.addedDate == null)
			entity.addedDate = new Date();
		entity = repository.save(entity);
		return ExecuteResult.ok(entity);
	}

	public ExecuteResult<?> delete(String id) {
		repository.delete(id);
		return ExecuteResult.ok();
	}

}
